var app = getApp(),
  $ = require("../../utils/httpHelper.js")
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
// 实例化API核心类
var demo = new QQMapWX({
  key: 'WSSBZ-5HXWF-FB3JM-JW5ZC-AML57-4BFO3' // 必填
});

Page({
  data: {
    showdialog: true,
    mblng:'108.90412',
    mblat:'34.24019',
    height:0,
    markers:[],
    // markers: [{
    //   iconPath: "/assets/weizhi.png",
    //   latitude: 34.24019,
    //   longitude: 108.90412,
    // }],
    loca:[
      {
        longitude:'108.90412',
        latitude:'34.24019'
      },//高新2路2号
      {
        longitude: '108.89694',
        latitude: '34.23624'
      },//高新三路3号
      {
        longitude: '108.89906',
        latitude: '34.24193'
      },//高新四路4号
      {
        longitude: '108.89241',
        latitude: '34.21293'
      },//高新路
      {
        longitude: '108.94194',
        latitude: '34.31417'
      },//凤城一路
    ]
  },
  onLoad: function (e) {
    var self = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        self.setData({
          height: res.windowHeight - 50,
        })
      }
    })
    this.getshop()
  },
  getshop(){
    $.httpPost("mendian_list", { rd_session: app.globalData.userInfo.rd_session}, (data, err) => {
      let shops = data.list
      let locals = []
      for(let local of shops){
        if (local.latitude && local.longitude){
          let po = {}
          po.latitude = local.latitude
          po.longitude = local.longitude
          locals.push(po)
        }
      }
      console.log(locals)
      this.setData({
        loca: locals
      })
    })
  },
  searchdo(e) {
    let str = e.detail.value
    console.log(str)
    this.getloca(str)
  },
  //目标地址转坐标
  getloca(str){
    var that = this;
    demo.geocoder({
      address: str,
      complete: function (res) {
        console.log(res)
        if(res.message == 'query ok'){
          that.setData({
            mblng: res.result.location.lng,
            mblat: res.result.location.lat
          })
          that.getdismark()
        }
        
      }
    });
  },
  //获取目标点三公里内的mark
  getdismark(){
    var that = this;
    demo.calculateDistance({
      from:{
        longitude:that.data.mblng,
        latitude:that.data.mblat
      },
      to: that.data.loca,
      complete: function (res) {
        console.log(res);
        if (res.message == 'query ok') {
          that.getdismark2(res.result.elements)
        }else{
          wx.showToast({
            title: res.message,
          })
        }
      }
    });
  },
  //计算公里内
  getdismark2(arr){
    let markarr = []
    for(let item of arr){
      if (item.distance <= 3000){
        let mark = {}
        mark.iconPath = "/assets/weizhi.png"
        mark.latitude = item.to.lat
        mark.longitude = item.to.lng
        markarr.push(mark)
      }
    }
    if (markarr.length == 0){
      wx.showToast({
        title: '没有店铺',
      })
    }
    this.setData({
      markers: markarr

    })
    console.log(this.data.markers)
  },
  showDialog() {
    this.setData({
      showdialog: true
    })
  },
  formSubmit: function (e) {
    var rd_session = wx.getStorageSync('rd_session');
    if (!app.check_empty(e.detail.value.username) && !app.check_empty(e.detail.value.pwd)){
      var param = { rd_session: rd_session, name: e.detail.value.username, password: e.detail.value.pwd }
      $.httpPost("check_priv", param, (data, err) => {
        if (data.data.result) {
          this.setData({
            showdialog: false
          })
        } else {
          wx.showToast({
            title: '用户名密码错误',
          })
        }

      })
    }else{
      wx.showToast({
        title: '不能是空',
      })
    }
    
  },
  formReset: function () {
    wx.switchTab({
      url: '/pages/mine/mine',
    })
  },
  

});